Skip to content

Allow subclass in equals in Default implementations#248

Open
mhrimaz wants to merge 4 commits into
eclipse-aas4j:mainfrom
mhrimaz:mhrimaz/equals
Open

Allow subclass in equals in Default implementations#248
mhrimaz wants to merge 4 commits into
eclipse-aas4j:mainfrom
mhrimaz:mhrimaz/equals

Conversation

@mhrimaz

@mhrimaz mhrimaz commented Feb 11, 2024

Copy link
Copy Markdown

The current implementation of equals function is too restrict and does not allow comparing different subclasses of an entity to be comparable. This PR solves this. However, it does not solve the ordering of collections mentioned in #50

Furthermore, the motivation behind allowing custom subclasses should be explained, and such implementation considerations should be explained.

@twebermartins twebermartins added the V.1.1.0 Final Release V.1.1.0 incl. additional features label Feb 13, 2024
@twebermartins

twebermartins commented Nov 19, 2024

Copy link
Copy Markdown
Contributor

Is this still active?

@mhrimaz

mhrimaz commented Nov 19, 2024

Copy link
Copy Markdown
Author

@twebermartins probably the changes that I applied are on the codes generated automatically. So I assume the correct logic should be applied in the code generator.
In any case the issue still remains, but it is not something critical. You can see here an example test case:
https://github.com/eclipse-aas4j/aas4j/blob/fefa728bae84d8686c08af2553ca4f2cd6ee9cfd/dataformat-core/src/test/java/org/eclipse/digitaltwin/aas4j/v3/model/ModelTest.java

package org.eclipse.digitaltwin.aas4j.v3.model;

import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.CustomSubProperty;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty;
import org.junit.Test;

public class ModelTest {

    void fillProperty(Property object) {
        object.setIdShort("test");
        object.setValueType(DataTypeDefXsd.STRING);
    }

    @Test
    public void testPropertyEqualityWithSubclass() {
        Property defaultProperty = new DefaultProperty();
        Property customProperty = new CustomSubProperty();
        fillProperty(customProperty);
        fillProperty(defaultProperty);

        //object should be equal to itself
        assert defaultProperty.equals(defaultProperty);
        //defaultProperty should be equal with customProperty
        assert defaultProperty.equals(customProperty);
        assert customProperty.equals(customProperty);
        assert customProperty.equals(defaultProperty);
        //hashCode implementation should be consistent
        assert customProperty.hashCode() == defaultProperty.hashCode();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

V.1.1.0 Final Release V.1.1.0 incl. additional features

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

2 participants